<?php

namespace App\Store\Controllers\Wxapp;


use App\Store\Models\Wxapp\WxaccountTemplate;
use App\Store\Validate\Wxapp\WxTemplate as ValidateWxappWxTemplate;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Common\Services\Store\Common as StoreCommon;
use App\Store\Models\Wxapp\WxaccountSetting;
use App\Store\Models\Wxapp\WxappSetting;
use App\Store\Validate\Wxapp\WxSetting as ValidateWxappWxSetting;


class WxSettingController extends Controller
{
    public function getwxaccountsetting(Request $request)
    {
        $accountinfo = StoreCommon::getCurrentAccountinfo($request);
        $cp_id = $accountinfo['cp_id'];
        $info = WxaccountSetting::where([['cp_id', $cp_id]])->first();

        return response()->json(['code' => '000', 'msg' => '获取成功', 'data' => ['info' => $info]]);
    }

    public function postwxaccountsetting(Request $request)
    {

        $req_data = $request->only(['appid', 'appsecret', 'token', 'encodingaeskey', 'remark', 'welcomemsg']);
        $Validate = new ValidateWxappWxSetting();
        if (!$Validate->check($req_data)) {
            return response()->json(['code' => '001', 'msg' => $Validate->getError()]);
        }
        $accountinfo = StoreCommon::getCurrentAccountinfo($request);
        $cp_id = $accountinfo['cp_id'];
        $info = WxaccountSetting::where([['cp_id', $cp_id]])->first();
        $data = $req_data;
        $data['remark'] = isset($data['remark']) ? $data['remark'] : '';

        $data['update_at'] = time();
        if ($info) {
            WxaccountSetting::where([['cp_id', $cp_id]])->update($data);
        } else {
            $data['cp_id'] = $cp_id;
            $data['create_at'] = time();
            WxaccountSetting::create($data);
        }
        return response()->json(['code' => '000', 'msg' => '保存配置成功']);
    }
    public function getwxappsetting(Request $request)
    {
        $accountinfo = StoreCommon::getCurrentAccountinfo($request);
        $cp_id = $accountinfo['cp_id'];
        $info = WxappSetting::where([['cp_id', $cp_id]])->first();

        return response()->json(['code' => '000', 'msg' => '获取成功', 'data' => ['info' => $info]]);
    }

    public function postwxappsetting(Request $request)
    {
        $req_data = $request->only(['appid', 'appsecret', 'wxpay_appid', 'wxpay_mchid', 'wxpay_key', 'wxpay_cert_path', 'wxpay_key_path', 'remark', 'contact_phone', 'contact_wxaccount','wxpay_platform_certs_path']);
        $Validate = new ValidateWxappWxSetting('wxapp');
        if (!$Validate->check($req_data)) {
            return response()->json(['code' => '001', 'msg' => $Validate->getError()]);
        }
        $accountinfo = StoreCommon::getCurrentAccountinfo($request);
        $cp_id = $accountinfo['cp_id'];
        $info = WxappSetting::where([['cp_id', $cp_id]])->first();
        $data = $req_data;
        $data['remark'] = isset($data['remark']) ? $data['remark'] : '';
        $data['wxpay_appid'] = $data['appid'];
        $data['update_at'] = time();
        if (!file_exists(storage_path() . $req_data['wxpay_cert_path']) || !is_file(storage_path() . $req_data['wxpay_cert_path'])) {
            return response()->json(['code' => '001', 'msg' => '无效cert证书']);
        }

        if (!file_exists(storage_path() . $req_data['wxpay_key_path']) || !is_file(storage_path() . $req_data['wxpay_key_path'])) {
            return response()->json(['code' => '001', 'msg' => '无效key证书']);
        }

        if ($info) {
            WxappSetting::where([['cp_id', $cp_id]])->update($data);
        } else {
            $data['cp_id'] = $cp_id;
            $data['create_at'] = time();
            WxappSetting::create($data);
        }

        return response()->json(['code' => '000', 'msg' => '保存配置成功']);
    }

    /**
     * 获取微信消息模版
     */
    public function getwxaccounttemplate(Request $request)
    {
        $accountinfo = StoreCommon::getCurrentAccountinfo($request);
        $cp_id = $accountinfo['cp_id'];
        $info = WxaccountTemplate::where([['cp_id', $cp_id]])->first();

        return response()->json(['code' => '000', 'msg' => '获取成功', 'data' => ['info' => $info]]);
    }

    /**
     * 保存微信消息模版
     */
    public function postwxaccounttemplate(Request $request)
    {
        $req_data = $request->only(['3_day', '3_week', '3_month', 'birthday', 'score', 'sales_order', 'deposit_order', 'recharge_order', 'recovery_order']);
        $Validate = new ValidateWxappWxTemplate();
        if (!$Validate->check($req_data)) {
            return response()->json(['code' => '001', 'msg' => $Validate->getError()]);
        }
        $accountinfo = StoreCommon::getCurrentAccountinfo($request);
        $cp_id = $accountinfo['cp_id'];
        $info = WxaccountTemplate::where([['cp_id', $cp_id]])->first();
        $data = $req_data;
        $data['birthday'] =$req_data['birthday']?? '';

        $data['update_at'] = time();
        if ($info) {
            WxaccountTemplate::where([['cp_id', $cp_id]])->update($data);
        } else {
            $data['cp_id'] = $cp_id;
            $data['create_at'] = time();
            WxaccountTemplate::create($data);
        }
        return response()->json(['code' => '000', 'msg' => '保存配置成功']);
    }
}
